import api from '../../utils/api.js';
import cache from '../../utils/cache.js';
import http from '../../utils/http.js';
import tools from '../../utils/tools.js';
const Http = new http;
// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    historyList: [],
    hotList: [],
    searchValue: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getHistoryList();
  },
  getHistoryList(e) {
    Http.httpReq({
      url: api.getHistoryList,
      data: {
        openid: cache.get('openid')
      },
      method: 'POST'
    }).then(res => {
      let data = res.data.data;

      if (data.history.length > 0) {
        this.setData({
          historyList: data.history
        })
      }
      if (data.hot.length > 0) {
        this.setData({
          hotList: data.hot
        })
      }
    })
  },
  hanlderClick(e) {
    let searchValue = e.currentTarget.dataset.search
    this.search(searchValue);
  },
  onBlur(e) {
    let searchValue = e.detail.value
    this.search(searchValue)
  },
  search(searchValue) {
    Http.httpReq({
      url: api.saveHistory,
      data: {
        openid: cache.get('openid'),
        search_value: searchValue
      },
      method: 'POST'
    }).then(res => {
      if (res.data.code == 1) {
        wx.navigateTo({
          url: '/pages/searchResult/searchResult?searchValue=' + searchValue,
        })
      }
    })
  },
  onClear() {
    Http.httpReq({
      url: api.clearHistory,
      data: {
        openid: cache.get('openid')
      },
      method: 'POST'
    }).then(res => {
      this.setData({
        historyList: []
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getHistoryList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})